﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace PaiBanDaPing
{
    public class ImageHelper
    {

        [DllImport("gdi32.dll", SetLastError = true)]

        private static extern bool DeleteObject(IntPtr hObject);

        public static ImageSource ChangeBitmapToImageSource(Bitmap bitmap)
        {
            IntPtr hBitmap = bitmap.GetHbitmap();
            ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                hBitmap,
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());
            bitmap.Dispose();
            if (!DeleteObject(hBitmap))
            {
                throw new System.ComponentModel.Win32Exception();
            }
            return wpfBitmap;
        }
        public static ImageSource ChangeBitmapToImageSource(Bitmap bitmap, int width, int height)
        {
            IntPtr hBitmap = bitmap.GetHbitmap();
            ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                hBitmap,
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromWidthAndHeight(width, height));
            bitmap.Dispose();
            return wpfBitmap;
        }

        public static Bitmap GetScreenBitmap(Screen screen)
        {
            //获取整个屏幕图像,不包括任务栏
            System.Drawing.Rectangle ScreenArea = screen.WorkingArea;
            Bitmap bmp = new Bitmap(ScreenArea.Width, ScreenArea.Height);
            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.CopyFromScreen(screen.WorkingArea.X, 0, 0, 0, new System.Drawing.Size(ScreenArea.Width, ScreenArea.Height));
                g.Dispose();
            }

            return bmp;
        }
        public static Bitmap GetScreenBitmap(Screen screen, int width, int height)
        {
            //获取整个屏幕图像,不包括任务栏
            System.Drawing.Rectangle ScreenArea = screen.WorkingArea;
            Bitmap bmp = new Bitmap(ScreenArea.Width, ScreenArea.Height);
            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.CopyFromScreen(screen.WorkingArea.X, 0, 0, 0, new System.Drawing.Size(ScreenArea.Width, ScreenArea.Height));
                g.Dispose();
            }
            bmp = KiResizeImage(bmp, width, height);
            return bmp;
        }

        public static Bitmap KiResizeImage(Bitmap bmp, int newW, int newH)
        {
            try
            {
                Bitmap b = new Bitmap(newW, newH);
                Graphics g = Graphics.FromImage(b);

                g.InterpolationMode = InterpolationMode.HighQualityBicubic;

                g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
                g.Dispose();

                return b;
            }
            catch
            {
                return null;
            }
        }
    }
}
